package com.sig.opengl;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;

public class MyGLSurfaceView extends GLSurfaceView{

	public MyGLRenderer _renderer = null;

	@Override
	public void onResume(){	 
		super.onResume();
		_renderer.onResume();
	}

	public MyGLSurfaceView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		creer();
	}

	public void changerCamera(float x_tmp,float y_tmp , float z_tmp ){
		MyGLRenderer.ancien_x = MyGLRenderer.x;
		MyGLRenderer.ancien_y = MyGLRenderer.y;
		MyGLRenderer.ancien_z = MyGLRenderer.z;
		
		MyGLRenderer.x = x_tmp ;
		MyGLRenderer.y = y_tmp;
		MyGLRenderer.z = z_tmp;	
		requestRender();
	}
	public MyGLSurfaceView(Context context) {
		super(context);
		creer();
	}

	public MyGLRenderer getRenderer(){
		return _renderer;
	}

	private void creer(){
		setEGLContextClientVersion(2);

		//disabling z-buffrer
		setEGLConfigChooser(false);
		
		_renderer = new MyGLRenderer();
		setRenderer(_renderer);

		setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
	}

	boolean bite = false;	

	ScaleGestureDetector _scaleDetector = null;

	float oldX, oldY;
	float oldViewX, oldViewY;

	public static int INVALID_POINTER_ID = 2323;
	// The ‘active pointer’ is the one currently moving our object.
	public int mActivePointerId = INVALID_POINTER_ID;
	
	boolean zoom = false;

	@Override
	public boolean onTouchEvent(MotionEvent e) {
		zoom = false;

		if(_scaleDetector == null){
			_scaleDetector = new ScaleGestureDetector(getContext(), new OnScaleGestureListener() {
				float old_z;
				float new_z;

				@Override
				public void onScaleEnd(ScaleGestureDetector detector) {
					if(new_z > 0){
						MyGLRenderer.z = this.new_z;
						zoom = true;
					}
				}	
				@Override
				public boolean onScaleBegin(ScaleGestureDetector detector) {
					this.old_z = MyGLRenderer.z;
					zoom = true;
					return true;
				}
				
				@Override
				public boolean onScale(ScaleGestureDetector detector) {

					float scale = detector.getScaleFactor();					
					zoom = true;

					this.new_z = this.old_z / scale;

					if(new_z > 0){
						MyGLRenderer.z = new_z;
						//Log.d("GL", "z: " + _renderer.z);
						MyGLSurfaceView.this.requestRender();
					}
					return false;
				}
			});
		}

		_scaleDetector.onTouchEvent(e);
		
		if(!zoom){
			final int action = MotionEventCompat.getActionMasked(e); 

			switch (action) { 
			case MotionEvent.ACTION_DOWN: {

				final int pointerIndex =(action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
						>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;      
						final float x = e.getX(pointerIndex);
						final float y = e.getY(pointerIndex);

						// Remember where we started (for dragging)
						oldViewX = x;
						oldViewY = y;
						// Save the ID of this pointer (for dragging)
						mActivePointerId = MotionEventCompat.getPointerId(e, 0);
						break;
			}
			case MotionEvent.ACTION_UP: {
				mActivePointerId = INVALID_POINTER_ID;
				break;
			}
			case MotionEvent.ACTION_CANCEL: 
				break;

			default:

				final int pointerIndex =(action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
				>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;      
				float x = e.getX(pointerIndex);
				float y = e.getY(pointerIndex);

				// Calculate the distance moved
				float dx = (x - oldViewX)/getWidth();
				float dy = (y - oldViewY)/getHeight();

				float z = MyGLRenderer.z;
				//acceleration en fonction de Z
				dx = dx*z;
				dy = dy*z;

				MyGLRenderer.x -= dx;
				MyGLRenderer.y += dy;

				requestRender();

				// Remember this touch position for the next move event
				oldViewX = x;
				oldViewY = y;

				break;
			}       
		}
		return true;

	}

}
